package com.lzq.model.flyweight;

import java.util.HashMap;

/**
 * @author : modify
 * @version : v1.0
 * @description :
 * @createTime : 2022/11/24 11:18
 * @updateUser : modify
 * @updateTime : 2022/11/24 11:18
 * @updateRemark :
 */
public abstract class AbstractBox {
    /**
     * 获取形状
     *
     * @return String
     */
    protected abstract String getShape();

    public void display(String color) {
        System.out.println("形状 ：" + getShape() + " , 颜色：" + color);
    }
}

class IBox extends AbstractBox {

    @Override
    public String getShape() {
        return "I";
    }
}

class LBox extends AbstractBox {

    @Override
    public String getShape() {
        return "L";
    }
}

class OBox extends AbstractBox {

    @Override
    public String getShape() {
        return "O";
    }
}

class BoxFactory {
    private static HashMap<String, AbstractBox> map;

    private BoxFactory() {
        map = new HashMap<>();
        map.put("i", new IBox());
        map.put("o", new OBox());
        map.put("l", new LBox());
    }

    public static final BoxFactory getInstance() {
        return SingletonHolder.INSTANCE;
    }

    private static class SingletonHolder {
        private static final BoxFactory INSTANCE = new BoxFactory();
    }

    public AbstractBox getBox(String key) {
        return map.get(key);
    }
}

class Test {
    public static void main(String[] args) {
        AbstractBox ibox = BoxFactory.getInstance().getBox("i");
        ibox.display("blue");
        AbstractBox lbox = BoxFactory.getInstance().getBox("l");
        lbox.display("red");
    }
}